package com.icbc.util;

public class RSAUtil {

		  
 public static String pubkeyEncrypt(String pubkey,String content) throws Exception{
		
        System.out.println("--------------公钥加密私钥解密过程-------------------");  
        //公钥加密过程  
        byte[] cipherData=RSAEncrypt.encrypt(RSAEncrypt.loadPublicKeyByStr(pubkey),content.getBytes());  
        String cipher=RSABase64Util.encode(cipherData);
		return cipher;
	}
 
//公钥解密过程 
 public static String prikeyDecrypt(String prikey,String content) throws Exception{
	 byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPrivateKeyByStr(prikey), RSABase64Util.decode(content));  
	 String restr=new String(res); 
	 return restr;
	}
 
 public static String prikeyEncrypt(String prikey,String content) throws Exception{
		
     System.out.println("--------------公钥加密私钥解密过程-------------------");  
     //公钥加密过程  
     byte[] cipherData=RSAEncrypt.encrypt(RSAEncrypt.loadPrivateKeyByStr(prikey),content.getBytes());  
     String cipher=RSABase64Util.encode(cipherData);
		return cipher;
	}

//公钥解密过程 
public static String pubkeyDecrypt(String pubkey,String content) throws Exception{
	 byte[] res=RSAEncrypt.decrypt(RSAEncrypt.loadPublicKeyByStr(pubkey), RSABase64Util.decode(content));  
	 String restr=new String(res); 
	 return restr;
	}


 
	
public static void main(String[] args) throws Exception {  
	         
	        String pubkey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQytSfFiiKaeYk18VswgMn52Mq/S0yJO7ZsS1LRS9WQtwfXFTIim1Hd94msjw3aRR2SmJ3hbrJlFNdeBJuCxr7puHcJcpPnUOpNLkianRGr6ionpxbZ3YKojdfLZiv/k2ksEIq4Fgn/z1UlbSNx7If4c5Af9h09iN6uFEc23WtGQIDAQAB";
	        String prikey="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJDK1J8WKIpp5iTXxWzCAyfnYyr9LTIk7tmxLUtFL1ZC3B9cVMiKbUd33iayPDdpFHZKYneFusmUU114Em4LGvum4dwlyk+dQ6k0uSJqdEavqKienFtndgqiN18tmK/+TaSwQirgWCf/PVSVtI3Hsh/hzkB/2HT2I3q4URzbda0ZAgMBAAECgYBgbrYtPhTCGsWsxAg45v4zFWh2uSfgvbhWRCpmdy2PxffU6ymMZqVhc/QA0bRJMzLtlZz+/ZHpr1/WYKHlqH/PEF30ps+7yFBUmVKqx8s2HnEuShbpo9ydqFKJs8eQTtgzDLg4o+IAeEZ0WI9PRYpV6Y3qBbuUb3AoPxwHxn8oAQJBAP5iXwbEldCe/wOYAmkQbH7RriPX1KE+5yEePljqjO8BOQdMWQsxlf7XyDmrjdgp9h5OvERr7U1oYg6eOxKLb7UCQQCRtkNJab7p2cwbILnAIoz/D5WIoDAYYkwD0YPj+A9oxgc4eMqJK5y7qfqJDsWhrkodwFcSqe/COpUB4oNdhP5VAkBX3MpNoCDOydvy9dH/T/jpZKrtHmMGvp8+RR/rvjenZZi4+JnYz+uq9gTmRmbbvsKPfUpxp4VzMxaLTgPBR//xAkBTZVKrgMdQxmPIHvnRGrVCAiRedvgv7Ip/IPnwjvgaykQ1sQSSb5Cb49RNf1PjjoJC9uOe/f4IV0kTNBZlfwmlAkEAlymuH+XxQQFmZOIeWeoSUUIq24hT1+EnMiQU3qdx5yuH5iOp2arxz8DuxSbxIJJovd6PKglwcWDhQKaXq+l57g==";
	        System.out.println("--------------公钥加密私钥解密过程-------------------");  
	        String plainText="ihep_公钥加密私钥解密";  
	        //公钥加密过程  
	        String cipher=RSAUtil.pubkeyEncrypt(pubkey,plainText);  
	        //私钥解密过程  
	        String restr=RSAUtil.prikeyDecrypt(prikey, cipher);  
	        
//	        System.out.println("原文："+plainText);  
//	        System.out.println("加密："+cipher);  
//	        System.out.println("解密："+restr);  
//	        System.out.println();  
//	          
//	        System.out.println("--------------私钥加密公钥解密过程-------------------");  
//	        plainText="ihep_私钥加密公钥解密";  
//	        //私钥加密过程  
//	       // cipherData=RSAEncrypt.encrypt(RSAEncrypt.loadPrivateKeyByStr(prikey),plainText.getBytes());  
//	        cipher=RSAUtil.prikeyEncrypt(prikey,plainText);  
//	        //公钥解密过程  
//	        restr=RSAUtil.pubkeyDecrypt(pubkey, cipher);  
//	         
//	        System.out.println("原文："+plainText);  
//	        System.out.println("加密："+cipher);  
//	        System.out.println("解密："+restr);  
//	        System.out.println();  
//	          
//	        System.out.println("---------------私钥签名过程------------------");  
//	        String content="ihep_这是用于签名的原始数据";  
//	        String signstr=RSASignature.sign(content,prikey);  
//	        System.out.println("签名原串："+content);  
//	        System.out.println("签名串："+signstr);  
//	        System.out.println();  
//	          
//	        System.out.println("---------------公钥校验签名------------------");  
//	        System.out.println("签名原串："+content);  
//	        System.out.println("签名串："+signstr);  
//	          
//	        System.out.println("验签结果："+RSASignature.doCheck(content, signstr, pubkey));  
//	        System.out.println();  
//	          
	    }  
	}  


